<?php
namespace jy_newshopfee;
defined('BY_JYA') or exit('error');
/*
	分销插件工具类
*/
class model extends \plugin{
	
	public function create_condition($conditions){
		if(!$conditions){
			return '';
		}
		if(!is_array($conditions)){
			$conditions=json_decode($conditions,true);
		}
		$level_up_types=$this->level_up_types;
		$current_level_up_types=$this->current_level_up_types();
		$str='';
		foreach($current_level_up_types as $key){
			$str.=$level_up_types[$key].'达'.$conditions[$key].';';
		}
		return $str;
	}
	public function current_level_up_types(){
		return str_array($this->setting['level_up_types']);
	}
	public function add_upfee(){
		$params=array('uniacid'=>$_SESSION['uniacid']);
		$parameter=array(
			'params'=>$params,
			'limit'=>'1',
			'orderby'=>'id desc',
		);
		$list=$this->get_list('upfee',$parameter,$total);
		if(count($list)>0){
			$start_time=$list[0]['end_time'];
		}else{
			$parameter['orderby']='id asc';
			$list=$this->get_list('commission',$parameter,$total);
			if(count($list)>0){
				$start_time=$list[0]['createtime'];
			}else{
				return false;
			}
		}
		$start_time=strtotime(date("Y-m-d",$start_time));
		//获取周期；
		$up_days=$this->setting['up_days']?:7;
		$up_days=intval($up_days)?:7;
		$end_time=($start_time+$up_days*24*3600)-1;
		
		if($end_time>TIMESTAMP){
			return false;
		}
		$params['createtime between']=array($start_time,$end_time);
		$commissions=$this->getall('commission',$params);
		if(count($commissions)==0){
			return false;
		}
		$newfee=array();
		foreach($commissions as $cv){
			$newfee[$cv['mid']]['order'][]=$cv;
			$newfee[$cv['mid']]['agent_id']=$this->getcolumn('member',array('mid'=>$cv['mid']),'agent_id');
		}
		$agentfee=array();
		foreach($newfee as $mid=>$nv){
			foreach($nv['order'] as &$ov){
				if($ov['type']==0){
					$sale_num +=1;
					$sale_fee +=$ov['money'];
				}elseif($ov['type']==2){
					$jicha_num +=1;
					$jicha_fee +=$ov['money'];
				}elseif($ov['type']==3){
					$fugou_num +=1;
					$fugou_fee +=$ov['money'];
				}
			}
			$fee=$jicha_fee+$sale_fee+$fugou_fee;
			$feedata=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$mid,
				'jicha_num'=>$jicha_num,
				'jicha_fee'=>$jicha_fee,
				'sale_num'=>$sale_num,
				'sale_fee'=>$sale_fee,
				'fugou_num'=>$fugou_num,
				'fugou_fee'=>$fugou_fee,
				'uplevel_fee'=>0,
				'level_fee'=>0,
				'word_fee'=>0,
				'fee'=>$fee,
				'status'=>0,
				'start_time'=>$start_time,
				'end_time'=>$end_time,
			);
			$this->insert('upfee',$feedata);
			if($nv['agent_id']){
				$agentfee[$nv['agent_id']]=$agentfee[$nv['agent_id']]?:array('fee'=>0);
				$agentfee[$nv['agent_id']]['fee']=$agentfee[$nv['agent_id']]['fee']+$fee;
			}
		}
		//计算其他奖励
		foreach($agentfee as $mid=>$nnv){
			$level_id=$this->getcolumn('member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),'level_id');
			if(!$level_id){
				continue;
			}
			$level=$this->get('level',array('id'=>$level_id,'uniacid'=>$_SESSION['uniacid']));
			$commissions=$level['commissions']?json_decode($level['commissions'],true):array();
			if(!$commissions['chaoyue'] && !$commissions['levelfee']){
				continue;
			}
			$uplevelrate=intval($commissions['chaoyue']);
			$uplevel_fee=$nnv['fee']*$uplevelrate;
			$levelrate=intval($commissions['levelfee']);
			$level_fee=$nnv['fee']*$levelrate;
			$addfee=$uplevel_fee+$level_fee;
			$update=array(
				'uplevel_fee'=>$uplevel_fee,
				'level_fee'=>$level_fee,
				'fee +='=>$addfee,
			);
			$this->update('upfee',$update,array('end_time'=>$end_time,'uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
		}
		return true;
	}
}